import { Component,OnInit } from '@angular/core';
import { InfiniteScrollCustomEvent } from '@ionic/angular';
import { Router } from '@angular/router';


@Component({
  selector: 'app-shop',
  templateUrl: './shop.component.html',
  styleUrls: ['./shop.component.scss']
})
export class ShopComponent implements OnInit  {
  shopData: any = []

  ngOnInit(){
    this.generateCard();
  }
  async generateCard() {
      let list = await this.getShopList()
      console.log(list);
      this.shopData = this.shopData.concat(list); 
      //this.MediumData = [...this.MediumData ,...list]; 
  }
  onIonInfinite(ev: any) {
    this.generateCard();
    setTimeout(() => {
      (ev as InfiniteScrollCustomEvent).target.complete();
    }, 500);
  }

  async getShopList(){
    let result = await fetch("http://metapunk.cn:9999/parse/classes/BRBshop?limit=11", {
      "headers": {
        "accept": "*/*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "if-none-match": "W/\"164-Qy3vijCmC7iE0mND19p6WsFbmSU\"",
        "x-parse-application-id": "dev"
      },
      "referrer": "http://localhost:4040/",
      "referrerPolicy": "strict-origin-when-cross-origin",
      "body": null,
      "method": "GET",
      "mode": "cors",
      "credentials": "omit"
    });
    let data = await result.json();
    let list = data?.results
    return list

  }
  public text: string[] = []; // 假设这是一个字符串数组

  public results = [...this.text];

  handleInput(event: any) {
    console.log('数据前')

    console.log(this.text)
    console.log('数据后')
    let query = event.target.value.toLowerCase();
    this.results = this.text.filter((d) => d.toLowerCase().indexOf(query) > -1);
    console.log(this.results)
    //数组
    //显示这些文字对于的card
    let shopDataTemp: any = []
    for (let shop of this.shopData) {
      //console.log(medium.text)
      for(let t of this.results){
        console.log(t)
        if(shop.text == t){
          shopDataTemp.push(shop)
        }
      }
    }
    this.shopData = shopDataTemp;
    console.log(this.shopData)
  }

  constructor(private router: Router) {
    this.shopData = [
    ]
  }
  
  goDetail() {
    this.router.navigate(["/BigRedBook/shop/detail"], {
      queryParams: {}
    }
    );
  }
}
